<template>
  <a-spin :spinning="loading">
    <div>
      <a-form-model
        class="fromModels"
        :model="config"
        ref="realnameModel"
        layout="vertical"
        :labelCol="{ span: 6 }"
        :wrapperCol="{ span: 8 }"
      >
        <a-form-model-item label="实名认证返佣开关">
          <a-radio-group v-model="status">
            <a-radio :value="1"> 开 </a-radio>
            <a-radio :value="0"> 关 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="税费费率">
          <a-input v-model="tax_rate">
            <div slot="addonAfter">万</div>
          </a-input>
        </a-form-model-item>
        <a-form-model-item
          v-for="(item, index) in config.profit"
          :key="index"
          :prop="'profit.' + index"
          :label="index + '级返佣金额'"
          :rules="[{ required: true, message: '请输入' + index + '级返佣金额', trigger: 'blur' }]"
        >
          <a-input v-model="config.profit[index]">
            <div slot="addonAfter">元</div>
          </a-input>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 8 }">
          <a-button type="primary" @click="lineChanges('add')">新增一行</a-button>
          <a-button type="primary" @click="lineChanges('del')">删除最后一行</a-button>
        </a-form-model-item>
        <a-form-model-item :wrapper-col="{ span: 12, offset: 6 }">
          <a-button type="primary" @click="whites">提交</a-button>
          <a-button type="primary" @click="getPrivacyAgreementInfo">重置</a-button>
        </a-form-model-item>
      </a-form-model>
    </div>
  </a-spin>
</template>

<script>
import { getRebateApi, setRebateApi } from '@/api/return.js'
export default {
  name: 'realname',
  data() {
    return {
      loading: false,
      config: {
        profit: []
      },
      tax_rate: 0,
      status: 1
    }
  },
  created() {
    this.getPrivacyAgreementInfo()
  },
  methods: {
    lineChanges(type) {
      if (type == 'add') {
        this.config.profit.push('')
      }
      if (type == 'del') {
        this.config.profit.splice(this.config.profit.length - 1, 1)
      }
    },
    getPrivacyAgreementInfo() {
      this.loading = true
      getRebateApi({
        type: 'real_person_rebate',
        brand_id: process.env.VUE_APP_BRAND_ID
      })
        .then(res => {
          this.config.profit = res.data.config.profit
          this.tax_rate = res.data.config.tax_rate
          this.status = res.data.status
        })
        .finally(() => {
          this.loading = false
        })
    },
    whites() {
      this.$refs.realnameModel.validate(valid => {
        if (valid) {
          this.setDatas()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    setDatas() {
      setRebateApi({
        type: 'real_person_rebate',
        status: this.status,
        brand_id: process.env.VUE_APP_BRAND_ID,
        data: {
          profit: this.config.profit,
          tax_rate: this.tax_rate
        }
      }).then(res => {
        this.getPrivacyAgreementInfo()
        this.$message.success('操作成功')
      })
    }
  }
}
</script>

<style lang="less" scoped>
.btnStyle {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
